package com.neuedu.io;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public class FileDemo {
    public static void main(String[] args) {
        File  file = new File("d:\\");
        System.out.println(File.separator);
        System.out.println(File.separatorChar);
        //代表D盘,file代表，目录或者是文件
        File  fileD = new File("d:\\");
        File  fileDF = new File("d:\\file");
        // 文件
        File  txtA = new File("d:\\a.txt");
        File  txtB = new File("d:\\","b.txt");

        URI uri = URI.create("file:///D:/");
        File txtc = new File(uri);
        File txtd = new File(fileDF.getPath(),"d.txt"); //  d:/files/d.txt

        System.out.println("fileD  d:\\\t" +  fileD.exists());
        System.out.println("fileDF d:\\file\\\t"+ fileDF.exists() );
        System.out.println("txtA d:\\a.txt\t"+ txtA.exists() );
        System.out.println("txtB d:\\b.txt\t"+ txtB.exists() );
        System.out.println("txtc d:\\\t"+ txtc.exists() );
        System.out.println("txtd d:\\file\\d.txt\t"+ txtd.exists() );
        System.out.println("===============");
        System.out.println("FileD  文件："+ fileD.isFile()+ "-- 目录:"+fileD.isDirectory());
        System.out.println("fileDF  文件："+ fileDF.isFile()+ "-- 目录:"+fileDF.isDirectory());
        System.out.println("txtA  文件："+ txtA.isFile()+ "-- 目录:"+txtA.isDirectory());
        System.out.println("txtd  文件："+ txtd.isFile()+ "-- 目录:"+txtd.isDirectory());
        System.out.println("==================================");
        try {
            if(!txtA.exists()) {
                boolean isCreateSuccess = txtA.createNewFile();
                System.out.println(isCreateSuccess);
                System.out.println(txtA.getAbsolutePath());
                System.out.println(txtA.getPath());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if(!fileDF.exists()){
//            try {
//                boolean  success = fileDF.createNewFile();
                boolean  success  = fileDF.mkdir();
                System.out.println(success);
                System.out.println(fileDF.getAbsolutePath());
                System.out.println(fileDF.getPath());
//            }catch (IOException e){
//                throw  new RuntimeException(e);
//            }

        }
        System.out.println("============================");
        File  fcde = new File("d:\\c\\d\\e");
        if(!fcde.exists()) {
            boolean success = fcde.mkdirs();//  多级目录
            System.out.println(success);
            System.out.println(fcde.getAbsolutePath());
            System.out.println(fcde.getPath());
        }
        System.out.println("==========临时文件夹=============");

        try {

            File   filetemp=  File.createTempFile("new",".txt");
            File   filetemp2=  File.createTempFile("new",".txt",new File("D:\\"));
            System.out.println("filetemp:" + filetemp.getAbsolutePath());
            System.out.println("filetemp2:" + filetemp2.getAbsolutePath() );

//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }

            System.out.println("filetemp.delete():"+filetemp.delete());
            System.out.println("filetemp2.delete():"+filetemp2.delete());
            System.out.println("(\"d:\\\\abc.txt\").delete()" + new File("d:\\abc.txt").delete());
            //System.out.println("txtA  d:a.txt: delete() : " + txtA.delete());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.println("canRead:"+txtA.canRead());
        System.out.println("canWrite:"+txtA.canWrite());
        System.out.println("canExecute:"+txtA.canExecute());

        System.out.println("======================");
        File   fileDiectroy = new File("D:\\workspace\\jshand\\");
        String  []  children = fileDiectroy.list();/// 字符串形式 返回文件名的集合
        for (String  name: children
             ) {
            System.out.println(name);
        }
        System.out.println("======================");
        File []  files = fileDiectroy.listFiles();// 文件形式  返回的是文件对象
        for (File  f : files
             ) {
            System.out.println(f.getName());
        }

        File txtE =  new File("d:\\e.txt");
        boolean  success = txtE.renameTo(new File("d:\\renameE.txt"));
        System.out.println(success);
    }
}
